﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Dropthings.DAL;

namespace Dropthings.Business.Facade
{
    partial class Facade
    {
        public string[] EcBasisInMonth(int year, int month, string identifyCode)
        {
            
            string[] Ecsasis=new string[]{"",""};
            double thisyearmoney=getMoneyInMonth(year, month, identifyCode);
            double lastyearmoney = getMoneyInMonth(year-1, month, identifyCode);
            int lastmonthyear = year;
            int lastmonth = month - 1;
            if (lastmonth <= 0)
            {
                lastmonth = 12;
                lastmonthyear = year - 1;
            }
            double lastmonthmoney = getMoneyInMonth(lastmonthyear, lastmonth, identifyCode);
            string yearbyyearBasis = "";
            string monthbymonthBasis = "";
            if (lastyearmoney != 0)
            {
                yearbyyearBasis = Math.Round(((thisyearmoney - lastyearmoney)*100 / lastyearmoney),2).ToString();
            }
            if (lastmonthmoney != 0)
            {
                monthbymonthBasis = Math.Round(((thisyearmoney - lastmonthmoney)*100 / lastmonthmoney), 2).ToString();
            }
            Ecsasis[0]=yearbyyearBasis;
            Ecsasis[1] = monthbymonthBasis;
            return Ecsasis;
        }

        private double getMoneyInMonth(int year, int month, string identifyCode)
        {
            double money_thismonth = 0;
            double money_Ammeter = 0;
            double money_Water = 0;
            double money_Gas = 0;
            UQ_GetAmmeterMoneyInMonth query_ammeter = new UQ_GetAmmeterMoneyInMonth(year, month, identifyCode);
            DataSet ds_ammeter = query_ammeter.ExecuteDataset();
            if (ds_ammeter != null && ds_ammeter.Tables.Count > 0 && ds_ammeter.Tables[0].Rows.Count > 0)
                money_Ammeter = Convert.ToDouble(ds_ammeter.Tables[0].Rows[0]["Money"]);
            UQ_GetWaterMoneyInMonth query_water = new UQ_GetWaterMoneyInMonth(year, month, identifyCode);
            DataSet ds_water = query_water.ExecuteDataset();
            if (ds_water != null && ds_water.Tables.Count > 0 && ds_water.Tables[0].Rows.Count > 0)
                money_Water = Convert.ToDouble(ds_water.Tables[0].Rows[0]["Money"]);
            UQ_GetGasMoneyInMonth query_gas = new UQ_GetGasMoneyInMonth(year, month, identifyCode);
            DataSet ds_gas = query_water.ExecuteDataset();
            if (ds_gas != null && ds_gas.Tables.Count > 0 && ds_gas.Tables[0].Rows.Count > 0)
                money_Gas = Convert.ToDouble(ds_gas.Tables[0].Rows[0]["Money"]);
            money_thismonth=money_Ammeter+money_Water+money_Gas;
            return money_thismonth;
        }

    }
}
